package com.naizhuai.ui.components;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.naizhuai.R;
import com.naizhuai.util.ScreenUtils;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import rx.Subscription;

/**
 * @author：lichenxi
 * @date 2018/2/24 12
 * email：525603977@qq.com
 * Fighting
 */
public class TipDialog extends Dialog {

    private Subscription subscription;
    private Activity context;

    public TipDialog(@NonNull Context context) {
        super(context, R.style.TipDialog);
        this.context= (Activity) context;
    }

    public TipDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.context= (Activity) context;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void dismiss() {
        if(subscription!=null){
            if(!subscription.isUnsubscribed())
                subscription.unsubscribe();
        }
        super.dismiss();
    }

    public Subscription getSubscription() {
        return subscription;
    }

    public void setSubscription(Subscription subscription) {
        this.subscription = subscription;
    }

    @Override
    public void onBackPressed() {
        context.onBackPressed();
    }

    //默认一个图标 一个文字的样式 暂时先写一个 后面有啥再加啥
    public static class  Builder{
         public static final int ICON_NOTHING=0;

         public static final int ICON_LOADING=1;
         public static final int ICON_TYPE_FAIL = 2;
        @IntDef({ICON_NOTHING,ICON_LOADING,ICON_TYPE_FAIL})
        @Retention(RetentionPolicy.SOURCE)
        public @interface IconType{

        }
        private Context mContext;
        private CharSequence mTipText;

        private @IconType int currentIconType=ICON_NOTHING;
        public Builder(Context context){
            this.mContext=context;
        }

        public Builder setIconType(@IconType int iconType){
             currentIconType=iconType;
            return this;
        }
         public Builder setTipText(CharSequence tipText) {
             mTipText = tipText;
             return this;
         }

         public TipDialog create(){
            TipDialog tipDialog=new TipDialog(mContext);
            tipDialog.setCancelable(false);
            tipDialog.setContentView(R.layout.commom_dialog_layout);
            ViewGroup contentWrap=tipDialog.findViewById(R.id.contentWrap);

            if (currentIconType==ICON_LOADING){
                LoadingView loadingView = new LoadingView(mContext);
                loadingView.setColor(Color.WHITE);
                loadingView.setSize(ScreenUtils.dp2px(mContext, 32));
                LinearLayout.LayoutParams loadingViewLP = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                loadingView.setLayoutParams(loadingViewLP);
                contentWrap.addView(loadingView);
            }else if (currentIconType == ICON_TYPE_FAIL) {
                ImageView imageView = new ImageView(mContext);
                LinearLayout.LayoutParams imageViewLP = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                imageView.setLayoutParams(imageViewLP);
                imageView.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.icon_notify_error));
                contentWrap.addView(imageView);

            }


            if (mTipText!=null && mTipText.length()>0){
                TextView tipView=new TextView(mContext);
                LinearLayout.LayoutParams tipViewLP = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                if (currentIconType != ICON_NOTHING) {
                    tipViewLP.topMargin = ScreenUtils.dp2px(mContext, 12);
                }
                tipView.setLayoutParams(tipViewLP);

                //设置文字溢出隐藏
                tipView.setEllipsize(TextUtils.TruncateAt.END);
                tipView.setGravity(Gravity.CENTER);
                tipView.setMaxLines(2);
                tipView.setTextColor(ContextCompat.getColor(mContext, R.color.white));
                tipView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
                tipView.setText(mTipText);
                contentWrap.addView(tipView);
            }
            return tipDialog;
         }
    }
}
